<template>
	<view class="userinfo__page">
		<view class="block__group">
			<view class="item">
				<text>头像</text>
				<image class="avatar__item" :src="avatar"></image>
			</view>
		</view>
		<view class="block__group">
			<view class="item">
				<text>昵称</text>
				<text>{{nickname}}</text>
			</view>
			<view class="item">
				<text>性别</text>
				<text>{{gender | sex}}</text>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		filters: {
			sex(sex) {
				const arr = ['未知', '男', '女'];
				return arr[sex];
			}
		},
		data() {
			return {
				userId: null,
				userInfo: null
			}
		},
		computed: {
			avatar() {
				const defaultAvatar = 'https://miva-unmcc.oss-cn-beijing.aliyuncs.com/avatar.png';
				return this.userInfo && this.userInfo.avatarUrl || defaultAvatar;
			},
			nickname() {
				return this.userInfo && this.userInfo.nickName;
			},
			gender() {
				return this.userInfo && this.userInfo.gender || 0;
			}
		},
		mounted() {
			this.userId = uni.getStorageSync('userId');
			if (this.userId) {
				this.getUserInfoAPI(this.userId); //获取用户信息
			}
		},
		methods: {
			async getUserInfoAPI(userId) {
				const [err, res] = await this.$https({
					url: '/wechat/userinfo',
					method: 'POST',
					data: {
						userId: userId
					}
				})
				this.userInfo = res.data.data;
				return res.data.data;
			}
		}
	}
</script>

<style lang="scss">
	.userinfo__page {
		min-height: 100vh;
		padding: 20px;
		box-sizing: border-box;
		background-color: #F1F1F1;
	}

	.block__group {
		margin-bottom: 20px;
		padding: 30px;
		border-radius: 10px;
		background-color: #FFFFFF;

		.item {
			padding: 10px;
			margin-bottom: 10px;
			display: flex;
			justify-content: space-between;
			align-items: center;
			border-bottom: 1px solid #ddd;
			font-size: 16px;

			text:last-child {
				font-size: 14px;
				color: #666;
			}
		}

		.avatar__item {
			width: 50px;
			height: 50px;
			border-radius: 50%;
		}
	}
</style>
